﻿using HardcoreGame.Common.CustomSets;
using HardcoreGame.Content.Items.Weapons.Ranged.DoubleGun;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;
using Terraria;
using Terraria.GameContent;
using Terraria.ModLoader;
using Terraria.UI;

namespace HardcoreGame.Common.UI
{
    public class DoubleGunUI : UIState
    {
        public override void Draw(SpriteBatch spriteBatch)
        {
            Player player = Main.LocalPlayer;
            if (player.HeldItem != null && CustomItemSets.DoubleGun[player.HeldItem.type])
            {
                if (player.HeldItem.ModItem is BaseDoubleGun gun && gun.Ammo != null)
                {
                    Vector2 basePos = player.Top - Main.screenPosition - new Vector2(50, 20);
                    DrawCustomBar(spriteBatch, basePos, gun.Ammo.CurrentBullets[0], gun.Ammo.MaxBullet, "Ⅰ", gun.Ammo.IsLiving);
                    DrawCustomBar(spriteBatch, basePos + new Vector2(0, 14), gun.Ammo.CurrentBullets[1], gun.Ammo.MaxBullet, "Ⅱ", !gun.Ammo.IsLiving);
                }
            }
        }
        private static void DrawCustomBar(SpriteBatch spriteBatch, Vector2 position, int current, int max, string label, bool isActive)
        {
            const int barWidth = 100;
            const int barHeight = 6;
            float ratio = (float)current / max;
            Color fillColor = Color.Lerp(Color.Red, Color.LimeGreen, ratio);
            Color bgColor = isActive ? new Color(100, 100, 100, 150) : new Color(50, 50, 50, 100);
            Color labelColor = isActive ? Color.White : Color.Gray;
            var bgRect = new Rectangle((int)position.X, (int)position.Y, barWidth, barHeight);
            spriteBatch.Draw(TextureAssets.MagicPixel.Value, bgRect, bgColor);
            if (current > 0)
            {
                var fillRect = new Rectangle((int)position.X, (int)position.Y, (int)(barWidth * ratio), barHeight);
                spriteBatch.Draw(TextureAssets.MagicPixel.Value, fillRect, fillColor);
            }
            if (isActive)
            {
                var indicatorRect = new Rectangle((int)position.X - 3, (int)position.Y, 2, barHeight);
                spriteBatch.Draw(TextureAssets.MagicPixel.Value, indicatorRect, Color.Gold);
            }
            Utils.DrawBorderString(spriteBatch, label, position + new Vector2(barWidth + 8, -3), labelColor, 0.8f);
        }
    }
    public class OnyxDoubleGunUISystem : ModSystem
    {
        private UserInterface onyxDoubleGunInterface;
        internal DoubleGunUI onyxDoubleGunUI;

        public override void Load()
        {
            if (!Main.dedServ)
            {
                onyxDoubleGunUI = new DoubleGunUI();
                onyxDoubleGunInterface = new UserInterface();
                onyxDoubleGunInterface.SetState(onyxDoubleGunUI);
            }
        }

        public override void UpdateUI(GameTime gameTime)
        {
            onyxDoubleGunInterface?.Update(gameTime);
        }

        public override void ModifyInterfaceLayers(List<GameInterfaceLayer> layers)
        {
            int resourceBarIndex = layers.FindIndex(layer => layer.Name.Equals("Vanilla: Resource Bars"));
            if (resourceBarIndex != -1)
            {
                layers.Insert(resourceBarIndex, new LegacyGameInterfaceLayer(
                    "HardcoreGame: Double Gun UI",
                    delegate
                    {
                        Player player = Main.LocalPlayer;
                        if (player.HeldItem != null && CustomItemSets.DoubleGun[player.HeldItem.type])
                        {
                            onyxDoubleGunUI.Draw(Main.spriteBatch);
                        }
                        return true;
                    },
                    InterfaceScaleType.UI)
                );
            }
        }
    }
}